package com.epalmpay.controller.channel.datareport;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.epalmpay.commom.BaseController;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.status.Rescode;
import com.epalmpay.dto.datareport.GroupAccountInDTO;
import com.epalmpay.dto.datareport.GroupAccountListOutDTO;
import com.epalmpay.dto.datareport.GroupAccountLogDayOutDTO;
import com.epalmpay.dto.datareport.GroupAccountLogDaySumOutDTO;
import com.epalmpay.dto.datareport.GroupAccountLogMonthOutDTO;
import com.epalmpay.dto.datareport.GroupAccountLogMonthSumOutDTO;
import com.epalmpay.dto.datareport.GroupAccountLogOutDTO;
import com.epalmpay.dto.datareport.GroupAccountOutDTO;
import com.epalmpay.dto.system.PageDTO;
import com.epalmpay.entity.GroupAccount;
import com.epalmpay.entity.GroupAccountLogDay;
import com.epalmpay.entity.GroupAccountLogDaySum;
import com.epalmpay.entity.GroupAccountLogMonth;
import com.epalmpay.entity.GroupAccountLogMonthSum;
import com.epalmpay.entity.Page;
import com.epalmpay.enumdef.BizEnum;
import com.epalmpay.service.groupaccount.IGroupAccountService;
import com.epalmpay.service.operator.ISysLogService;
import com.epalmpay.util.PropertyCopyUtil;
import com.epalmpay.util.Tools;

/** 
 * 说明：数据报表 :营销数据列表
 * 创建人：gwn
 * 创建时间：2017-06-15
 */
@Controller
@RequestMapping(value="/merchant/groupAccount")
public class GroupAccountController  extends BaseController{
	
	@Resource
	private IGroupAccountService groupAccountService;
	
	@Resource
	private ISysLogService sysLogService;
	
	/**
	 * 显示营销数据列表
	 * @param 
	 * @param pageDTO
	 * @return
	 */
	@RequestMapping(value="/list")
	@ResponseBody
	public GroupAccountListOutDTO  list (GroupAccountInDTO groupAccountInDTO,PageDTO pageDTO){
		GroupAccountListOutDTO groupAccountListOutDTO=new GroupAccountListOutDTO();
		try {
			Page page=Tools.pageEmtity(pageDTO);
			
			//查询营销账户余额
			GroupAccount groupAccount = groupAccountService.getAmount(groupAccountInDTO);
			
			//获取月营销数据列表
			if (groupAccountInDTO.getQueryDayOrMonth()!=1) {
				
				List<GroupAccountLogMonthSumOutDTO> groupAccountMonthList = groupAccountService.getGroupAccountMonthList(groupAccountInDTO,page);	
				groupAccountListOutDTO.setGroupAccountMonthSumOutDTO(groupAccountMonthList);
			}else{
				//获取日营销数据列表
				List<GroupAccountLogDaySumOutDTO> groupAccountDayList = groupAccountService.getGroupAccountDayList(groupAccountInDTO,page);	
				
				groupAccountListOutDTO.setGroupAccountDaySumOutDTO(groupAccountDayList);
			}
			
			groupAccountListOutDTO.setGroupAccount(groupAccount);
			
			groupAccountListOutDTO.setPage(page); 
			groupAccountListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return groupAccountListOutDTO;
		} catch (ApplicationException ae) { 
			groupAccountListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}
		 catch (Exception e) { 
			 groupAccountListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return groupAccountListOutDTO;
	}		
	
	/**
	 * 查看详情
	 * @param 
	 * @param pageDTO
	 * @return
	 */
	@RequestMapping(value="/queryDetailList")
	@ResponseBody
	public GroupAccountListOutDTO  queryDetailList (GroupAccountInDTO groupAccountInDTO,PageDTO pageDTO){
		GroupAccountListOutDTO groupAccountListOutDTO=new GroupAccountListOutDTO();
	
		try {
			Page page=Tools.pageEmtity(pageDTO);
			
			if (groupAccountInDTO.getDaySumId()!=null) {
				
				GroupAccountLogDaySum groupAccountLogDaySum= new GroupAccountLogDaySum();
				groupAccountLogDaySum.setId(groupAccountInDTO.getDaySumId());
				
				groupAccountLogDaySum= groupAccountService.queryDaySumStatisDate(groupAccountLogDaySum);
				groupAccountInDTO.setStatisDate(groupAccountLogDaySum.getStatisDate());
				groupAccountListOutDTO.setGroupAccountLogDaySum(groupAccountLogDaySum);
				
				groupAccountInDTO.setAccountId(groupAccountLogDaySum.getAccountId());
				
				//日
				List<GroupAccountLogDayOutDTO> groupAccountLogDayOList = groupAccountService.getGroupAccountLogDayList(groupAccountInDTO,page);	
				groupAccountListOutDTO.setGroupAccountLogDayOutDTO(groupAccountLogDayOList);
			} else {

				GroupAccountLogMonthSum groupAccountLogMonthSum= new GroupAccountLogMonthSum();
				groupAccountLogMonthSum.setId(groupAccountInDTO.getMonthSumId());
				groupAccountLogMonthSum= groupAccountService.queryMonthSumStatisDate(groupAccountLogMonthSum);
				
				groupAccountInDTO.setStringStatisDate(groupAccountLogMonthSum.getStatisDate());
				groupAccountInDTO.setAccountId(groupAccountLogMonthSum.getAccountId());
				
				List<GroupAccountLogMonthOutDTO> groupAccountMonthList = groupAccountService.getGroupAccountLogMonthList(groupAccountInDTO,page);
				groupAccountListOutDTO.setGroupAccountLogMonthOutDTO(groupAccountMonthList);
			}
			

			groupAccountListOutDTO.setPage(page); 
			groupAccountListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return groupAccountListOutDTO;
		} catch (ApplicationException ae) { 
			groupAccountListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}
		 catch (Exception e) { 
			 groupAccountListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return groupAccountListOutDTO;
	}		
	
	

	/**
	 * 查看消费返红包数据详情
	 * @param 
	 * @param pageDTO
	 * @return
	 */
	@RequestMapping(value="/queryDetailRedPack")
	@ResponseBody
	public GroupAccountListOutDTO  queryDetailRedPack (GroupAccountInDTO groupAccountInDTO,PageDTO pageDTO){
		GroupAccountListOutDTO groupAccountListOutDTO=new GroupAccountListOutDTO();
		try {
			Page page=Tools.pageEmtity(pageDTO);
			
			
			//首行
			GroupAccountLogDay groupAccountLogDay =new GroupAccountLogDay();
			groupAccountLogDay =groupAccountService.queryAccountById(groupAccountInDTO.getId());
			
			groupAccountInDTO.setStatisDate(groupAccountLogDay.getStatisDate());
			groupAccountInDTO.setAccountId(groupAccountLogDay.getAccountId());
			groupAccountInDTO.setSubjectType(groupAccountLogDay.getSubjectType());
			groupAccountInDTO.setChangeType(groupAccountLogDay.getChangeType());

			
			//总条数
			
			List<GroupAccountLogOutDTO> groupAccountLogOutDTO = groupAccountService.getGroupAccountLogList(groupAccountInDTO,page);	
//			groupAccountListOutDTO= groupAccountService.getGroupAccountLogList(groupAccountInDTO,page);	
			
			groupAccountListOutDTO.setGroupAccountLogOutDTO(groupAccountLogOutDTO);
			
			groupAccountListOutDTO.setPage(page); 
			groupAccountListOutDTO.setGroupAccountLogDay(groupAccountLogDay);
			groupAccountListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return groupAccountListOutDTO;
		} catch (ApplicationException ae) { 
			groupAccountListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}
		catch (Exception e) { 
			groupAccountListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return groupAccountListOutDTO;
	}	
	
	
	
	/**
	 * 查看月下消费情况
	 * @param 
	 * @param pageDTO
	 * @return
	 */
	@RequestMapping(value="/queryMonthDetail")
	@ResponseBody
	public GroupAccountListOutDTO  queryMonthDetail (GroupAccountInDTO groupAccountInDTO,PageDTO pageDTO){
		GroupAccountListOutDTO groupAccountListOutDTO=new GroupAccountListOutDTO();
		try {
			Page page=Tools.pageEmtity(pageDTO);
			
			GroupAccountLogMonth groupAccountLogMonth =new GroupAccountLogMonth();
			groupAccountLogMonth=groupAccountService.queryMonthAccountById(groupAccountInDTO.getId());
			
			groupAccountInDTO.setAccountId(groupAccountLogMonth.getAccountId());
			groupAccountInDTO.setStringStatisDate(groupAccountLogMonth.getStatisDate()+0+0);
			groupAccountInDTO.setSubjectType(groupAccountLogMonth.getSubjectType());
			
			
			List<GroupAccountLogMonthOutDTO> groupAccountMonthList = groupAccountService.queryGroupAccountLogMonthList(groupAccountInDTO,page);
			//总条数
//			List<GroupAccountLogDayOutDTO> groupAccountLogDayOutDTO = groupAccountService.getGroupAccountLogMonthDayList(groupAccountInDTO,page);	
//			groupAccountListOutDTO.setGroupAccountLogDayOutDTO(groupAccountLogDayOutDTO);
			groupAccountListOutDTO.setGroupAccountLogMonthOutDTO(groupAccountMonthList);
			
			
			groupAccountListOutDTO.setGroupAccountLogMonth(groupAccountLogMonth);
			groupAccountListOutDTO.setPage(page); 
			groupAccountListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return groupAccountListOutDTO;
		} catch (ApplicationException ae) { 
			groupAccountListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}
		 catch (Exception e) { 
			 groupAccountListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return groupAccountListOutDTO;
	}	
	
	
	/**
	 * 营销费用准备添加 (余额)回显余额
	 * 
	 */
	@RequestMapping(value="/goAdd")
	@ResponseBody
	public GroupAccountListOutDTO  goAdd(GroupAccountInDTO groupAccountInDTO){
		
		GroupAccountListOutDTO groupAccountListOutDTO = new GroupAccountListOutDTO();
		try {
			//查询营销账户余额
			GroupAccount groupAccount = groupAccountService.getAmount(groupAccountInDTO);
			groupAccountListOutDTO.setGroupAccount(groupAccount);
			groupAccountListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return groupAccountListOutDTO;
		}
		catch (ApplicationException ae) { 
			groupAccountListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}
		 catch (Exception e) { 
			 groupAccountListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return groupAccountListOutDTO;
	}	
	
	
	/**
	 *  去发短信页面  
	 * @param 
	 * @return
	 */
	@RequestMapping(value="/queryData")
	@ResponseBody
	
	public GroupAccountOutDTO  queryData(GroupAccountInDTO groupAccountInDTO){
		GroupAccountOutDTO groupAccountOutDTO=new GroupAccountOutDTO();
		GroupAccount groupAccount = new GroupAccount();
		try {   
			groupAccount.setGroupId(groupAccountInDTO.getGroupId());
			
			GroupAccount groupAccounts=groupAccountService.queryData(groupAccount);//查询手机号
			if(groupAccounts!=null){
				PropertyCopyUtil.copyPropertiesExclude(groupAccounts,groupAccountOutDTO);
				groupAccountOutDTO.setErrorCode(Rescode.SUCCESS, "查询成功");
				return groupAccountOutDTO;
			}else{
				groupAccountOutDTO.setErrorCode("-1", "未查询到记录");
			}
		} catch (Exception e) { 
			groupAccountOutDTO.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
		}
		groupAccountOutDTO.setErrorCode("-2", "处理失败");
		return groupAccountOutDTO;
	}
	
	
	
	/**
	 * 修改营销费用余额
	 * @param 
	 * @return
	 */
	@RequestMapping(value="/updateAmount")
	@ResponseBody
	public GroupAccountOutDTO  updateMemberGrade(GroupAccountInDTO groupAccountInDTO){
		GroupAccountOutDTO groupAccountOutDTO=new GroupAccountOutDTO();
		
		try {   
//		   groupAccountService.updateAmount(groupAccountInDTO);
		   
		   
	      Integer count =  groupAccountService.updateAmount(groupAccountInDTO.getGroupId(),BizEnum.SaleAccountSubjectType.AddAmount, groupAccountInDTO.getAddBalance(), null, null, groupAccountInDTO.getDescInfo());
		   
		   
		   groupAccountOutDTO.setErrorCode(Rescode.SUCCESS, "修改成功");
			
			String content ="修改营销账户余额";
			sysLogService.addSysLog(groupAccountInDTO.getUserId(),groupAccountInDTO.getUserName(),null,content,groupAccountInDTO.getGroupId());
			
			return groupAccountOutDTO;
		}catch (ApplicationException ae) { 
			groupAccountOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}
		 catch (Exception e) { 
			 groupAccountOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return groupAccountOutDTO;
	}

	@RequestMapping(value="/queryAmount")
	@ResponseBody
	public GroupAccountListOutDTO  queryAmount (GroupAccountInDTO groupAccountInDTO){
		GroupAccountListOutDTO groupAccountListOutDTO=new GroupAccountListOutDTO();
		try {

			//查询营销账户余额
			GroupAccount groupAccount = groupAccountService.getAmount(groupAccountInDTO);

			groupAccount.setBalance(groupAccount.getBalance()-groupAccount.getLockAmount());
			groupAccountListOutDTO.setGroupAccount(groupAccount);

			groupAccountListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return groupAccountListOutDTO;
		} catch (ApplicationException ae) {
			groupAccountListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}
		catch (Exception e) {
			groupAccountListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return groupAccountListOutDTO;
	}


}
